package com.ruoyi.odorservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.Result;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.odorservice.entity.Industry;
import com.ruoyi.odorservice.mapper.IndustryMapper;
import com.ruoyi.odorservice.model.vo.IndustryCraftOdorVo;
import com.ruoyi.odorservice.model.vo.IndustryListVo;
import com.ruoyi.odorservice.service.IndustryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class IndustryServiceImpl extends ServiceImpl<IndustryMapper, Industry> implements IndustryService {

    @Override
    public Result<List<IndustryCraftOdorVo>> getIndustryInfo(Integer craftId, Integer industryId) {
        List<IndustryCraftOdorVo> industryCraftOdors = this.baseMapper.getIndustryCraftOdor(industryId, craftId);
        return Result.ok(industryCraftOdors);
    }
    @Override
    public List<Industry> searchIndustry(String key) {
        LambdaQueryWrapper<Industry> wrapper = new LambdaQueryWrapper<Industry>()
                .select(Industry::getId, Industry::getIndustryName)
                .like(StringUtils.isNotEmpty(key), Industry::getIndustryName, key)
                .last("limit 5");
        return list(wrapper);
    }

    @Override
    public Result<IndustryListVo> industryList(Integer page, Integer limit, String industryName) {
        List<Industry> industryList = this.baseMapper.selectIndustryPage((page - 1) * limit, limit, industryName);
        Integer total = this.baseMapper.selectIndustryPageCount(industryName);
        IndustryListVo industryListVo = new IndustryListVo(total, industryList);
        return Result.ok(industryListVo);
    }

    @Override
    @Transactional
    public Result<Boolean> addIndustry(Industry industry) {
        if(hasSameIndustryName(industry.getIndustryName(), industry.getId())) return Result.fail(false, "存在相同名称");
        this.baseMapper.insert(industry);
        return Result.ok(true);
    }

    @Override
    @Transactional
    public Result<Boolean> updateIndustry(Industry industry) {
        if(hasSameIndustryName(industry.getIndustryName(), industry.getId())) return Result.fail(false, "存在相同名称");
        this.baseMapper.updateById(industry);
        return Result.ok(true);
    }

    //是否存在相同名称的行业
    private Boolean hasSameIndustryName(String industryName, Integer industryId) {
        LambdaQueryWrapper<Industry> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Industry::getIndustryName, industryName);
        Industry industry = this.baseMapper.selectOne(lambdaQueryWrapper);
        return industry != null && !industry.getId().equals(industryId);
    }
}
